<template>
  <div>
    <van-icon @click="onCollected" :color='collected? "green" :"#777"' :name='collected? "star" :"star-o"' />
  </div>
</template>

<script>
import { addArticleCollected, deleteArticleCollected } from '@/api/article.js'
export default {
  data () {
    return {
    }
  },
  model: {
    // 属性解析为collected
    prop: 'collected'
  },
  props: {
    collected: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, Object],
      required: true
    }
  },
  methods: {
    async onCollected () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        if (this.collected) {
          // 取消收藏
          await deleteArticleCollected(this.artId)
        } else {
          // 收藏
          await addArticleCollected(this.artId)
        }
        this.$toast('操作成功')
        this.$emit('input', !this.collected)
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
